package com.newlink.building.record.adapter

import android.text.TextUtils
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.newlink.building.common_base.model.bean.CallRecordListData
import com.newlink.building.record.R
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author wangyang
 * @date 2022/9/29
 * @description 呼叫记录的Adapter
 */
class Module_Record_CallRecordAdapter(data: MutableList<CallRecordListData>, sip: String) :
    BaseQuickAdapter<CallRecordListData, BaseViewHolder>(
        R.layout.item_call_record, data
    ), LoadMoreModule {

    private val localSip = sip

    override fun convert(holder: BaseViewHolder, item: CallRecordListData) {
        if (item.callerNumber == localSip) {
            if (!TextUtils.isEmpty(item.answerNumber)) {
                holder.setText(R.id.tv_name, item.answerName)
            } else {
                holder.setText(R.id.tv_name, item.answerNumber)
            }
            holder.setText(R.id.tv_describe, item.answerNumber)
        } else {
            if (!TextUtils.isEmpty(item.callerName)) {
                holder.setText(R.id.tv_name, item.callerName)
            } else {
                holder.setText(R.id.tv_name, item.callerNumber)
            }
            holder.setText(R.id.tv_describe, item.callerNumber)
        }
        holder.setText(R.id.tv_time, item.startStamp)
//        holder.setText(R.id.tv_time, TimezoneConvert.convertTimeFromGMTToLocal(item.startStamp))
        val dateFormat = SimpleDateFormat(
            if (item.duration >= 3600) "HH:mm:ss" else "mm:ss",
            Locale.getDefault()
        )
        val cal = Calendar.getInstance()
        cal[0, 0, 0, 0, 0] = item.duration.toInt()
        val format = dateFormat.format(cal.time)
        holder.setText(R.id.tv_duration, format)

        /**
         * 挂断原因(
         * 0.正常结束通话
         * 1.用户按拒接键
         * 2.开启了DND
         * 3.在黑名单中
         * 4.port busy
         * 5.媒体协商出错）
         */
//        if (item.hangupCause in 1..5) {
//            holder.setBackgroundResource(R.id.iv_status, R.mipmap.call_record_reject)
//            holder.setTextColor(R.id.tv_name,
//                ContextCompat.getColor(context, com.vdroid.dooraccess.main.R.color.default_red))
//        } else {
//            if (item.callerNumber == localSip) {
//                holder.setBackgroundResource(R.id.iv_status, R.mipmap.call_record_outgoing)
//            } else {
//                //根据通话时长显示已接和未接
//                holder.setBackgroundResource(R.id.iv_status, if(item.duration == 0) R.mipmap.call_record_reject else R.mipmap.call_record_incoming)
//            }
//        }

        var textColorRes = R.color.default_text
        if (item.callerNumber == localSip) {
            holder.setBackgroundResource(R.id.iv_status, R.mipmap.call_record_outgoing)
        } else {
            if (item.duration == 0) {
//                textColorRes = R.color.default_red
                holder.setBackgroundResource(R.id.iv_status, R.mipmap.call_record_reject)
            } else {
                holder.setBackgroundResource(R.id.iv_status, R.mipmap.call_record_incoming)
            }
        }

        holder.setTextColor(
            R.id.tv_name,
            ContextCompat.getColor(context, textColorRes)
        )
        holder.setTextColor(
            R.id.tv_time,
            ContextCompat.getColor(context, textColorRes)
        )
//        holder.setTextColor(
//            R.id.tv_describe,
//            ContextCompat.getColor(context, textColorRes)
//        )
//        holder.setTextColor(
//            R.id.tv_duration,
//            ContextCompat.getColor(context, textColorRes)
//        )

    }
}